package authhandler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	p "yundian/internel/app/models/permissions"
	"yundian/internel/app/models/roles"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// ------- 角色相关API请求处理 roleHandler -------

// HandleGetRoleList 查询 - 有分页、有查询参数
func HandleGetRoleList(ctx *gin.Context) {
	// 请求参数处理
	param := &request.RoleListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	// 取出每个角色所拥有的权限
	data := dao.NewRoleDao().GetRoleList(param)
	lists := data.Unwrap().([]*roles.RoleWithPermission)

	if len(lists) > 0 {
		for _, list := range lists {
			lst := dao.NewPermissionDao().
				GetPermissionWithActionListFromRoleId(list.Id).Unwrap().([]*p.PsermissionWithActionList)
			if len(lst) > 0 {
				for _, l := range lst {
					act := helper.GenerateActionData(l.ActionData)
					l.ActionData = act
				}
				list.Permissions = lst
			} else {
				list.Permissions = lst
			}
		}
	}

	// 响应请求
	Response(ctx)("", CodeSuccess,
		ResponseList{
			PageNo:     param.PageNo,
			PageSize:   param.PageSize,
			TotalCount: data.Val().(int64),
			TotalPage:  0,
			Data:       lists,
		},
	)(OK)
}

// HandleGetRoleListNoPager 查询角色列表 - 无参数，无分页
func HandleGetRoleListNoPager(ctx *gin.Context) {
	param := &struct {
		IsAdmin int `json:"isAdmin" form:"isAdmin"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param))
	IsAdmin, _ := helper.GetIsAdminFromCtx(ctx)
	param.IsAdmin = int(IsAdmin)

	Response(ctx)("", CodeSuccess, dao.NewRoleDao().GetRoleListByIsAdmin(param.IsAdmin).Unwrap())(OK)
	//Response(ctx)("", CodeSuccess, dao.NewRoleDao().GetRoleListNoPager().Unwrap())(OK)
}

func HandleGetRoleListByIsAdmin(ctx *gin.Context) {
	param := &struct {
		IsAdmin int `json:"isAdmin" form:"isAdmin"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param))
	Response(ctx)("", CodeSuccess, dao.NewRoleDao().GetRolesByIsAdmin(param.IsAdmin).Unwrap())(OK)
}

func HandleGetStaffRoleList(ctx *gin.Context) {
	Response(ctx)("", CodeSuccess, dao.NewRoleDao().GetStaffRoleList().Unwrap())(OK)
}

// GetRole 查询 - 详情
func HandleGetRoleInfo(ctx *gin.Context) {

}

// HandleInsertRole 新增
func HandleInsertRole(ctx *gin.Context) {
	param := &request.RoleInsertReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	roleInfo := dao.NewRoleDao().InsertRole(param).Unwrap().(*roles.RolesModel)
	if roleInfo.Id > 0 && len(param.Permissions) > 0 {
		for _, p := range param.Permissions {
			if len(p.ActionList) > 0 {
				actList, _ := json.Marshal(p.ActionList)
				insertMap := map[string]interface{}{
					"role_id":       roleInfo.Id,
					"permission_id": p.Id,
					"action_list":   string(actList),
				}

				dao.NewRoleDao().InsertRolePermission(insertMap).Unwrap()
			}
		}
	}

	Response(ctx)("", CodeInsertSuccess, true)(OK)
}

// HandleUpdateRole 修改
func HandleUpdateRole(ctx *gin.Context) {
	param := &request.RoleInsertReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()

	Response(ctx)("", CodeUpdateSuccess, dao.NewRoleDao().UpdateRole(param).Unwrap())(OK)
}

// HandleUpdateRoleStatus 修改状态
func HandleUpdateRoleStatus(ctx *gin.Context) {
	param := &struct {
		Id     int  `json:"id" binding:"required,gt=0"`
		Status int8 `json:"status" binding:"required,gt=0"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	Response(ctx)("", CodeUpdateSuccess, dao.NewRoleDao().UpdateRoleStatus(param.Id, param.Status).Unwrap())(OK)
}

// HandleDeleteRole 删除
func HandleDeleteRole(ctx *gin.Context) {
	param := &struct {
		Id int `json:"id" binding:"required,gt=0"`
	}{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	Response(ctx)("", CodeDeleteSuccess, dao.NewRoleDao().DeleteRole(param.Id).Unwrap())(OK)
}
